home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Blender 2.49b / blender-2.49b-windows.exe / $_4_ / .blender / scripts / image_edit.py < prev    next >
Text File  |  2009-08-31  |  5KB  |  159 lines

  1. #!BPY
  2. """
  3. Name: 'Edit Externally'
  4. Blender: 242a
  5. Group: 'Image'
  6. Tooltip: 'Open in an application for editing. (hold Shift to configure)'
  7. """
  8.  
  9. __author__ = "Campbell Barton"
  10. __url__ = ["blender", "blenderartists.org"]
  11. __version__ = "1.0"
  12. __bpydoc__ = """\
  13. This script opens the current image in an external application for editing.
  14.  
  15. Usage:
  16. Choose an image for editing in the UV/Image view.
  17.  
  18. To configure the application to open the image with, hold Shift as you
  19. click on this menu item.
  20.  
  21. For first time users try running the default application for your
  22. operating system.  If the application does not open you can type in
  23. the full path.  You can choose that the last entered application will
  24. be saved as a default.
  25.  
  26. * Note, default commants for opening an image are "start" for win32
  27. and "open" for macos.  This will use the system default associated
  28. application.
  29. """
  30.  
  31. # ***** BEGIN GPL LICENSE BLOCK *****
  32. #
  33. # Script copyright (C) Campbell J Barton 2006
  34. #
  35. # This program is free software; you can redistribute it and/or
  36. # modify it under the terms of the GNU General Public License
  37. # as published by the Free Software Foundation; either version 2
  38. # of the License, or (at your option) any later version.
  39. #
  40. # This program is distributed in the hope that it will be useful,
  41. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  42. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  43. # GNU General Public License for more details.
  44. #
  45. # You should have received a copy of the GNU General Public License
  46. # along with this program; if not, write to the Free Software Foundation,
  47. # Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  48. #
  49. # ***** END GPL LICENCE BLOCK *****
  50. # --------------------------------------------------------------------------
  51.  
  52. import Blender
  53. from Blender import Image, sys, Draw, Registry
  54.  
  55. try:
  56.     import subprocess
  57.     import sys as py_sys
  58.     platform = py_sys.platform
  59. except:
  60.     Draw.PupMenu('Error: Recent version of Python not installed.')
  61.     subprocess=None
  62.  
  63. def os_run(appstring, filename):
  64.     '''
  65.     Run the app, take into account different python versions etc
  66.     looks like python 2.6 wants a list for 
  67.     '''
  68.     
  69.     # evil trick, temp replace spaces so we can allow spaces in filenames
  70.     # also allows multiple instances of %f
  71.     appstring = appstring.replace(' ', '\t')
  72.     appstring = appstring.replace('%f', filename)
  73.     appstring = appstring.split('\t')
  74.     
  75.     print ' '.join(appstring)
  76.     
  77.     try: # only python 2.6 wants a list?
  78.         p = subprocess.Popen(appstring)
  79.     except:
  80.         p = subprocess.Popen(' '.join(appstring))
  81.     
  82.  
  83. def edit_extern(image=None):
  84.     
  85.     if not image:
  86.         image = Image.GetCurrent()
  87.     
  88.     if not image: # Image is None
  89.         Draw.PupMenu('ERROR: Please select active Image.')
  90.         return
  91.     if image.packed:
  92.         Draw.PupMenu('ERROR: Image is packed, unpack before editing.')
  93.         return
  94.     
  95.     imageFileName = sys.expandpath( image.filename )
  96.     
  97.     if not sys.exists(imageFileName):
  98.         Draw.PupMenu('ERROR: Image path does not exist.')
  99.         return
  100.     
  101.     pupblock = [imageFileName.split('/')[-1].split('\\')[-1]]
  102.     
  103.     new_text= False
  104.     try:
  105.         appstring = Registry.GetKey('ExternalImageEditor', True)
  106.         appstring = appstring['path']
  107.         
  108.         # for ZanQdo if he removed the path from the textbox totaly. ;) - Cam
  109.         if not appstring or appstring.find('%f')==-1:
  110.             new_text= True
  111.     except:
  112.         new_text= True
  113.     
  114.     if new_text:
  115.         pupblock.append('first time, set path.')
  116.         if platform == 'win32':
  117.             # Example of path to popular image editor... ;-)
  118.             # appstring = '"C:\\Program Files\\Adobe\\Photoshop CS\\photoshop.exe" "%f"'
  119.             # Have to add "cmd /c" to make sure we're using Windows shell.
  120.             appstring = 'cmd /c start "" /B "%f"'
  121.         elif platform == 'darwin':
  122.             appstring = 'open "%f"'
  123.         else:
  124.             appstring = 'gimp %f'
  125.     
  126.     appstring_but = Draw.Create(appstring)
  127.     save_default_but = Draw.Create(0)
  128.     
  129.     pupblock.append(('editor: ', appstring_but, 0, 99, 'Path to application, %f will be replaced with the image path.'))
  130.     pupblock.append(('Set Default', save_default_but, 'Store this path in the blender registry.'))
  131.     
  132.     # Only configure if Shift is held,
  133.     if Blender.Window.GetKeyQualifiers() & Blender.Window.Qual.SHIFT:
  134.         if not Draw.PupBlock('External Image Editor...', pupblock):
  135.             return
  136.     
  137.     appstring = appstring_but.val
  138.     save_default= save_default_but.val
  139.     
  140.     if save_default:
  141.         Registry.SetKey('ExternalImageEditor', {'path':appstring}, True)
  142.     
  143.     if appstring.find('%f') == -1:
  144.         Draw.PupMenu('ERROR: No filename specified! ("%f")')
  145.         return
  146.     
  147.     # -------------------------------
  148.     
  149.     os_run(appstring, imageFileName)
  150.  
  151.  
  152.  
  153. def main():
  154.     edit_extern()
  155.     
  156.  
  157. if __name__ == '__main__' and subprocess:
  158.     main()
  159.